package cn.afactory.tygz.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.View;

import com.tendcloud.tenddata.TCAgent;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.SnsTopicInfo;
import cn.afactory.tygz.ui.base.BaseFragmentActivity;
import cn.afactory.tygz.ui.base.utils.NaviUtils;
import cn.afactory.tygz.ui.fragments.TopicDetailFragment;

/**
 * Created by Kenneth on 15/9/24.
 */
public class TopicDetailActivity extends BaseFragmentActivity {

    public final static String EXT_DATA_TAG = "snsTopicInfo";
    public final static String EXT_ID_TAG = "snsTopicInfoId";
    public final static String EXT_TITLE_TAG = "snsTopicInfoTitle";

    private TopicDetailFragment mFragment;
    private SnsTopicInfo snsTopicInfo;
    private long mTopicId;
    private String mTopicTitle;

    private int mPageIndex = 0;

    public void setPageIndex(int index) {
        mPageIndex = index;
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    @Override
    public void onResume() {
        super.onResume();
        TCAgent.onResume(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addTitleBar = false;
        setContentView(R.layout.ac_fragment);
        Intent intent = getIntent();
        if (intent.hasExtra(TopicDetailActivity.EXT_DATA_TAG)) {
            snsTopicInfo = (SnsTopicInfo)intent.getSerializableExtra(TopicDetailActivity.EXT_DATA_TAG);
        } else {
            snsTopicInfo = null;
        }

        if (intent.hasExtra(TopicDetailActivity.EXT_ID_TAG)) {
            mTopicId = intent.getLongExtra(TopicDetailActivity.EXT_ID_TAG, 0);
        } else {
            mTopicId = 0;
        }

        if (intent.hasExtra(TopicDetailActivity.EXT_TITLE_TAG)) {
            mTopicTitle = intent.getStringExtra(TopicDetailActivity.EXT_TITLE_TAG);
        } else {
            mTopicTitle = null;
        }

        initview();
    }

    /**
     * 跳转到话题详情页面
     */
    public static void gotoTopicDetailActivity(Context context,String topicTitle){
        Intent intent = new Intent(context,TopicDetailActivity.class);
        intent.putExtra(TopicDetailActivity.EXT_TITLE_TAG, topicTitle);
        context.startActivity(intent);
    }

    /**
     * 跳转到话题详情页面
     */
    public static void gotoTopicDetailActivity(Context context,long topicId){
        Intent intent = new Intent(context,TopicDetailActivity.class);
        intent.putExtra(TopicDetailActivity.EXT_ID_TAG, topicId);
        context.startActivity(intent);
    }

    /**
     * 跳转到话题详情页面
     */
    public static void gotoTopicDetailActivity(Context context,SnsTopicInfo snsTopicInfo){
        Intent intent = new Intent(context,TopicDetailActivity.class);
        intent.putExtra(TopicDetailActivity.EXT_DATA_TAG,snsTopicInfo);
        context.startActivity(intent);
    }


    private void initview() {
        mFragment = new TopicDetailFragment();
        if (mFragment != null) {
            Bundle bundle = new Bundle();
            if (null != snsTopicInfo) {
                bundle.putSerializable(TopicDetailActivity.EXT_DATA_TAG, snsTopicInfo);
            } else if (null != mTopicTitle){
                bundle.putString(TopicDetailActivity.EXT_TITLE_TAG, mTopicTitle);
            }
            bundle.putLong(TopicDetailActivity.EXT_ID_TAG, mTopicId);
            mFragment.setArguments(bundle);
            addFragment(mFragment);
        } else {
            finish();
        }
    }

    private void addFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fl_layout, fragment, "" + fragment.getClass())
                .commit();
    }
}